package org.demo.warehouse.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.demo.warehouse.common.pojo.Goods;
import org.demo.warehouse.common.utils.PageParam;
import org.demo.warehouse.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

public abstract class AbstractController<T> {
    @Autowired
    IService<T> service;

    @GetMapping("{id}")
    public R query(@PathVariable Long id) {
        return R.ok("data", service.getById(id));
    }

    @GetMapping("/page")
    public R page(PageParam param) {
        return R.ok(service.page(new Page<>(param.getPage(), param.getLimit()), param.getWrapper()));
    }

    @PostMapping
    public R save(T bean) {
        handleBeanBeforeSave(bean);
        return service.saveOrUpdate(bean) ? R.ok() : R.failed();
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        handleBeanBeforeDelete(id);
        return service.removeById(id) ? R.ok() : R.failed();
    }

    protected void handleBeanBeforeSave(T param) {

    }

    protected void handleBeanBeforeDelete(Long id) {
    }
}
